package wafteam.comicview;


import wafteam.database.Chap;
import wafteam.database.Database;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
	
	private ListView mainListView;
	final Context context = this;
	public final static String EXTRA_MESSAGE = "main2viewImage";
	private Database db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		db = new Database(MainActivity.this);
		Button view = (Button) findViewById(R.id.viewList);
		view.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				viewListChap();
			}
		});
		
		/*mainListView = (ListView) findViewById(R.id.mainListView);
		
		ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

		HashMap<String, String> map = new HashMap<String, String>();
		
		for (int i = 0; i < 10; i++) {
			map = new HashMap<String, String>();
			map.put("linkImages", "images");
			map.put("name", "ten chap");
			//map.put("soluongcon", arr[5]);
			//map.put("soluong", Main.arr[i][1]);
			menuItems.add(map);
		}
		
		ListAdapter adapter2 = new SimpleAdapter(this, menuItems,
				R.layout.list_item, new String[] { "listImage", "name" }, new int[] { R.id.ma, R.id.ten });
		
		// Set the ArrayAdapter as the ListView's adapter.
		mainListView.setAdapter(adapter2);
		mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, final View view, int position,
					long id) {
				
				//String a = position+ "";
				//goToViewImage(a);
				Database database = new Database(context);
				Chap chap = new Chap("tap 1",20, false, false, 100000);
				//contact.id = 
				database.addChap(chap);
				//database.createDB();
				
				
				TransData trans = new TransData();
				String data = trans.fetchJSON();
				
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						context);
				alertDialogBuilder.setTitle(data);
				alertDialogBuilder
				.setMessage(data)
				.setCancelable(false)
				.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, close
						// current activity
						//MainActivity.this.finish();
						dialog.cancel();
					}
				  })
				.setNegativeButton("No",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
					}
				});
 
				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();
 
				// show it
				alertDialog.show();
			}
			
		});*/
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void goToViewImage(String message){
		Intent intent = new Intent(this, ViewImageActivity.class);
		intent.putExtra(EXTRA_MESSAGE, message);
		startActivity(intent);
	}
	
	// Create some data to test
	public void createData(View view){
		Chap temp1 = new Chap("tap1", 13, 0, 0, 10);
		db.addChap(temp1);
		Chap temp3 = new Chap("tap3", 17, 0, 0, 15);
		db.addChap(temp3);
		Chap temp2 = new Chap("tap2", 21, 0, 0, 23);
		db.addChap(temp2);
	}
	public void destroyData(View view){
		db.destroyAll();
	}
	public void viewListChap(){
		Intent intent = new Intent(MainActivity.this, ChapterListActivity.class);
		startActivity(intent);
	}

}
